from moudles.Common.common import *
import traceback


def getImageArea(data: list):
    """
    判断图片格式所属省市区域
    Args:
        data (list): _description_
    """

    chongqinAreas, sichuanAreas = getArea()

    area = []
    isSkip = False
    for index, entry in enumerate(data):
        if isSkip:
            break

        text = entry[1][0]

        if not isSkip:
            for chongqin_item in chongqinAreas:
                if chongqin_item in text:
                    if '金域医学' in text:
                        area = ['chongqin', 'kingmend']
                    elif '妇幼保健院' in text:
                        area = ['chongqin', 'maternalChildHospital']
                    else:
                        area = ['chongqin', 'other']
                    isSkip = True
                    break

        if not isSkip:
            for sichuan_item in sichuanAreas:
                if sichuan_item in text:
                    if '天府新区人民医院' in text:
                        area = ['sichuan', 'maternalChildHospital']
                    elif '南充市中心医院' in text:
                        area = ['sichuan', 'maternalChildHospital']
                    isSkip = True
                    break
        # 结束
        if index > 6:
            isSkip = True
    return area


def getImageBlockData(result, split_posy, foortHeight, tipHeight):
    """
    获取区块数据
    Args:
        result (_type_): 图片的数据
        split_posy (_type_): 分割线开始的位置
        headerHeight (_type_): 图片头部基本数据的高度
        foortHeight (_type_): 图片尾部数据的高度
    Returns:
        _type_: _description_
    """
    # 初始化
    header_data_list = list()  # 头部消息
    header_title_data_list = list()  # 报告单所属医院信息
    header_base_data_list = list()  # 报告单用户信息
    main_desc_data_list = list()  # 描述信息
    tip_data_list = list()  # 提示信息
    footer_data_list = list()  # 脚本信息

    # 最后一个元素的Y轴坐标
    _, footer_end_posy = result[-1][0][0]

    # 描述开始的坐标
    main_base_start_posy = split_posy

    # 尾部开始的坐标
    footer_start_posy = footer_end_posy - foortHeight

    # 彩超提示开始的坐标
    tip_start_posy = footer_start_posy - tipHeight

    for index, itemList in enumerate(result):
        entry_posx, pentry_posy = itemList[0][0]
        entry_text = itemList[1][0]

        if pentry_posy <= main_base_start_posy:
            header_data_list.append(itemList)
        elif pentry_posy > main_base_start_posy and pentry_posy < tip_start_posy:
            main_desc_data_list.append(itemList)
        elif pentry_posy < footer_start_posy and pentry_posy >= tip_start_posy:
            tip_data_list.append(itemList)
        elif pentry_posy > footer_start_posy:
            footer_data_list.append(itemList)

    # 继续拆分头部信息，获取医院名称和用户信息
    # header_title_data_list = header_data_list[:7]  # 报告单所属医院信息
    # header_base_data_list = header_data_list[7:]  # 报告单用户信息
    siSkip = False
    hospital_name_posy = float()
    for index, header_data in enumerate(header_data_list):
        _posx, _posy = header_data[0][0]
        _text = header_data[1][0]

        # 医院名称默认在前6个信息中
        if not siSkip:
            zh_name_num = re.findall(r'[\u4e00-\u9fff]+', _text)  # 查找中文个数
            zh_name_str = ''.join(zh_name_num)
            if '医院' in zh_name_str and len(zh_name_str) >= 6:
                header_title_data_list.append(header_data)
                hospital_name_posy = _posy
                siSkip = True
        else:
            if _posy > hospital_name_posy+float(50):
                header_base_data_list.append(header_data)

    return header_title_data_list, header_base_data_list, main_desc_data_list, tip_data_list, footer_data_list
